package org.smile.transaction;

public class TransactionTemplate {
    /**
     * 处理业务
     * @param callback
     * @param <T>
     * @return
     * @throws Exception
     */
    public <T> T execute(TransactionCallback<T> callback) throws Exception {
        try{
            TransactionUtils.startManagered(true);
            T result= callback.doCallBack();
            TransactionUtils.commitAllTransactions();
            return result;
        }catch(Throwable e){
            TransactionUtils.rollBackAllTransactions();
            throw new TransactionException(e);
        }finally{
            TransactionUtils.endManagered();
            TransactionUtils.closeAllTransactions();
        }
    }
}
